// http://stackoverflow.com/questions/7365814/in-place-array-reordering
#include <iostream>

void reindex(int *input, int *index, size_t size)
{
    for (size_t i = 0; i < size; i++) {
        int x = input[i];
        int j = i;
        while (true) {
            int k = index[j]; // k is next index
            index[j] = j; // mark visited
            if (k == i) { // a loop is found
                break;
            }
            input[j] = input[k];
            j = k;
        }
        input[j] = x;
    }
}

int main()
{
    int input[] = { 7, 5, 2, 3, 1 };
    int index[] = { 1, 0, 3, 2, 4 };
    size_t size = sizeof(input)/sizeof(input[0]);
    reindex(input, index, size);
    for (size_t i = 0; i < size; i++) {
        std::cout << input[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}

